package com.jinlejia.erp.ui.common;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.GridLayoutManager;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.freedom.yefeng.yfrecyclerview.FullyGridLayoutManager;
import com.freedom.yefeng.yfrecyclerview.YfListRecyclerView;
import com.jinlejia.erp.R;
import com.jinlejia.erp.adapter.TagsAdapter;

import java.util.ArrayList;

/**
 * 标签选择对话框
 */

public class TagsDialog extends AlertDialog {
    ArrayList<String> data;
    TagsAdapter adapter;

    public TagsDialog(Context context, ArrayList<String> data) {
        super(context);
        this.data = data;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Window window = this.getWindow();
        // *** 主要就是在这里实现这种效果的.
        // 设置窗口的内容页面,shrew_exit_dialog.xml文件中定义view内容
        window.setContentView(R.layout.dialog_tags_select);
        window.setGravity(Gravity.BOTTOM);
        window.setBackgroundDrawable(new ColorDrawable());
        window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        //自定义
        TextView creat = (TextView) window.findViewById(R.id.bt_creat);
        //确定
        TextView confirm = (TextView) window.findViewById(R.id.bt_confirm);
        //查看数
        YfListRecyclerView recyclerView = (YfListRecyclerView) window.findViewById(R.id.yf_recycler);
        recyclerView.setLayoutManager(new FullyGridLayoutManager(window.getContext(), 4, GridLayoutManager.VERTICAL, false));

        adapter = new TagsAdapter(data);
        recyclerView.setAdapter(adapter);

        creat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AddTagsDialog addTagsDialog = new AddTagsDialog(window.getContext());
                addTagsDialog.setView(new EditText(window.getContext()));
                addTagsDialog.show();
            }
        });

        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if( null != selectListener )
                    selectListener.selected( adapter.getSelectStr() );

                dismiss();
            }
        });

    }

    public void setData(ArrayList<String> data) {
        if (null != adapter)
            adapter.setData(data);
    }


    public interface OnSelectListener {
        public void selected(String str);
    }

    private OnSelectListener selectListener;

    public void setOnSelectListener(OnSelectListener listener) {
        this.selectListener = listener;
    }
}
